package jinyilw.common.math;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;

public class MathTools // 程序与数学结合
{
	private MathTools()
	{
	}

	public static int[] getRandomDifferInts(int n, int length)
	{
		return getRandomDifferInts(0, n, length);
	}
	public static int[] getRandomDifferInts(int min, int max, int length)
	{
		int n = max - min + 1;
		if (n <= 0 || length <= 0)
			return null;
		if (length > n)
			length = n;
		int[] nums = new int[length];
		Random rand = new Random();
		boolean[] bool = new boolean[n];
		int randInt = 0;
		for (int i = 0; i < length; i++)
		{
			do
			{
				randInt = rand.nextInt(n);
			} while (bool[randInt]);
			bool[randInt] = true;
			nums[i] = randInt + min;
			// System.out.println(randInt);
		}
		return nums;
	}

	public static int[] getRandomDifferInts(int size)
	{
		if (size <= 0)
			return null;
		int[] values = new int[size];
		int temp1, temp2, temp3;
		Random rand = new Random();
		for (int i = 0; i < values.length; i++)
			values[i] = i;
		// 随机交换
		for (int i = 0; i < values.length; i++)
		{
			temp1 = rand.nextInt(values.length); // 随机产生一个位置
			temp2 = rand.nextInt(values.length); // 随机产生另一个位置
			if (temp1 != temp2)
			{
				temp3 = values[temp1];
				values[temp1] = values[temp2];
				values[temp2] = temp3;
			}
		}
		return values;
	}

	/**
	 * 保留小数点N位，请勿输入0
	 */
	public static double roundMid(Object num, int pointLen)
	{
		BigDecimal bg;
		if (num instanceof Double)
		{
			bg = BigDecimal.valueOf((double) num);
		} else if (num instanceof Long)
		{
			bg = new BigDecimal((long) num);
		} else
		{
			bg = new BigDecimal(num.toString());
		}
		return bg.setScale(pointLen, RoundingMode.DOWN).doubleValue();
	}

	public static String roundString(double num, int pointLen)
	{
		return String.format("%." + pointLen + "f", num);
	}

	public static int getMax(int... intArr)
	{
		int max = intArr[0];
		for (int value : intArr)
			if (value > max)
				max = value;
		return max;
	}

	public static int getMin(int... intArr)
	{
		int min = intArr[0];
		for (int value : intArr)
			if (value < min)
				min = value;
		return min;
	}

	public static int getMax(int[] intArr, int length)
	{
		int max = intArr[0];
		for (int i = 1; i < length; i++)
			if (intArr[i] > max)
				max = intArr[i];
		return max;
	}

	public static int[] getIncreaseIndexs(int... array)
	{
		if (array != null && array.length > 1)
		{
			int[] indexs = new int[array.length];
			for (int i = 0; i < indexs.length; i++)
				indexs[i] = i;
			int temp1, temp2, k;
			for (int i = 0; i < array.length; i++)
				for (int j = 0; j < array.length - 1 - i; j++)
					if (array[j + 1] < array[j])
					{
						k = j + 1;
						temp1 = array[k];
						array[k] = array[j];
						array[j] = temp1;
						temp2 = indexs[k];
						indexs[k] = indexs[j];
						indexs[j] = temp2;
					}
			return indexs;
		}
		return new int[0];
	}

	public static int[] getDecreaseIndexs(int... array)
	{
		if (array != null && array.length > 1)
		{
			int[] indexs = new int[array.length];
			for (int i = 0; i < indexs.length; i++)
				indexs[i] = i;
			int temp1, temp2, k;
			for (int i = 0; i < array.length; i++)
				for (int j = 0; j < array.length - 1 - i; j++)
					if (array[j + 1] > array[j])
					{
						k = j + 1;
						temp1 = array[k];
						array[k] = array[j];
						array[j] = temp1;
						temp2 = indexs[k];
						indexs[k] = indexs[j];
						indexs[j] = temp2;
					}
			return indexs;
		}
		return new int[0];
	}

	public static int[] getDecreaseIndexs(int num, int... intArr)
	{
		if (intArr != null && intArr.length > 1)
		{
			int[] copyArr = new int[intArr.length];
			System.arraycopy(intArr, 0, copyArr, 0, intArr.length);
			if (num > intArr.length)
				num = intArr.length;
			int[] indexs = new int[num];
			for (int i = 0; i < indexs.length; i++)
				indexs[i] = getMaxNumIndex(copyArr);

			return indexs;
		}
		return new int[0];
	}

	private static int getMaxNumIndex(int... intArr)
	{
		int max = intArr[0], maxNumIndex = 0;
		for (int i = 0; i < intArr.length; i++)
			if (intArr[i] > max)
			{
				max = intArr[i];
				maxNumIndex = i;
			}
		intArr[maxNumIndex] = Integer.MIN_VALUE;
		return maxNumIndex;
	}
}
